﻿Imports System.Media

Partial Public Class Logon
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then
            ' Dim xx As Double = CDbl(12.231)

            'Try
            '    Dim MyPlayer As New SoundPlayer()
            '    MyPlayer.SoundLocation = (Server.MapPath("../Music/")) & "testMusic.wav"
            '    MyPlayer.Load()
            '    MyPlayer.Play()
            '    MyPlayer.Dispose()

            'Catch ex As Exception

            'End Try
            Try
                Session.Clear()
                Session("Config") = clsGet.Get_Config()
                LiLogon.Text = CType(Session("Config"), DataSet).Tables(0).Rows(0).Item("Logon_Text")
            Catch ex As Exception
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ไม่สามารถเชื่อมต่อฐานข้อมูลได้ !!');", True)
            End Try
            'ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " window.open('../MasterPopup/PlayMusic.aspx','','width=320,height=25,top=700,left=1000');", True)
        End If
    End Sub


    Private Function ChkInput() As Boolean
        If (txtUsername.Text = "B") Or (txtUsername.Text = "L") Then
            If (txtPassword.Text = "kunskynoi") Then
                Dim dt As DataTable = clsGet.Get_Booking_Product_BP(txtUsername.Text)
                Dim conn As New System.Data.SqlClient.SqlConnection(connString)
                Dim clsDB As New ClassDatabase
                Dim clsFc As New ClassFunction
                Dim condition As Boolean = True
                Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
                Dim Booking As String = ""
                Dim ret As Integer = 0
                Application.Lock()
                conn.Open()
                tran = clsDB.BeginTran(conn)
                For Each dr As DataRow In dt.Rows
                    ret = clsFc.runupdate(dr.Item("Booking_no"), dr.Item("Product_no"), dr.Item("BP_Code"), conn, tran)
                    If (ret > 1) Then
                        Booking &= dr.Item("Booking_no") & ","
                    End If
                    ret = clsFc.runupdateBulk(dr.Item("Booking_no"), dr.Item("Product_no"), dr.Item("BP_Code"), conn, tran)
                Next
                If (condition) Then
                    clsDB.CommitTran(tran)
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('See Booking " & Booking & "!!');", True)
                Else
                    clsDB.RollBackTran(tran)
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก');", True)
                End If
                Application.UnLock()
                conn.Dispose()
            End If
        ElseIf (txtUsername.Text = "") Then
            txtCap.Text = String.Empty
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ Username !!');", True)
            Return False
        End If
        If (txtPassword.Text.Trim() = "") Then
            txtCap.Text = String.Empty
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ Password !!');", True)
            Return False
        End If
        'ccJoin.ValidateCaptcha(txtCap.Text)
        'If Not (ccJoin.UserValidated) Then
        '    txtCap.Text = String.Empty
        '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุรหัส Code ให้ถูกต้อง !!');", True)
        '    Return False
        'End If
        Return True
    End Function

    Private Sub ImgLogin_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImgLogon.Click
        If (ChkInput()) Then
            Dim clsEncrypt As New Class_Encrypt
            Try
                Dim ds As DataSet = clsGet.Get_Password(txtUsername.Text.ToLower().Trim())
                If Not (ds.Tables(0).Rows.Count = 0) Then
                    If (txtPassword.Text.Trim().ToLower() = clsEncrypt.Decrypt(ds.Tables(0).Rows(0).Item("Password").ToString())) Then
                        Session("UserData") = ds
                        Session("Config") = clsGet.Get_Config()
                        Session("UserMenuRole") = clsGet.LoadMenu(ds.Tables(0).Rows(0).Item("Role_no").ToString(), "''")
                        Session("Role") = ds.Tables(0).Rows(0).Item("Role_no").ToString()
                        Dim s As String = ds.Tables(0).Rows(0).Item("Role_no").ToString()

                        ' ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('../Form/MainPage.aspx','','left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no');window.open('', '_parent', '');window.close();", True)
                        Response.Redirect("../Form/MainPage.aspx", True)
                    Else
                        txtCap.Text = String.Empty
                        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('Username หรือ Password ผิดพลาด !!');", True)
                    End If
                Else
                    txtCap.Text = String.Empty
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('Username หรือ Password ผิดพลาด !!');", True)
                End If
            Catch ex As Exception
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ไม่สามารถเชื่อมต่อฐานข้อมูลได้ !!');", True)
            End Try

        End If
    End Sub

End Class